import { Component } from "@angular/core";
import { AlertController, NavParams, LoadingController } from "ionic-angular";
import { NavController } from "ionic-angular";
import { SignPage } from "../sign/sign";
import { Storage } from "@ionic/storage";
import { HttpService } from "../../providers/http.service";
import * as moment from "moment";
import { CommonFunc } from "../../providers/commonFunc";
import {
  FileUploadOptions,
  FileTransferObject,
  FileTransfer
} from "@ionic-native/file-transfer";
import { appConfig } from "../../providers/config";
/**
 * 工作总结
 */
@Component({
  templateUrl: "work-sum.html"
})
export class WorkSumPage {
  private param: any = {};
  //电梯信息
  private elevatorInfo: any;
  private maintenancenUsedTime: any;
  private date: any;
  private user: any;
  // private other: string;
  private inspectionSummary: string;
  private remarks: string;

  elevaType: any;

  tractionHalfMonBean: any = {};
  tractionQuarterBean: any = {};
  tractionHalfYearBean: any = {};
  tractionYearBean: any = {};

  hydpressHalfMonBean: any = {};
  hydpressHalfYearBean: any = {};
  hydpressQuarterBean: any = {};
  hydpressYearBean: any = {};

  debrisHalfMonBean: any = {};
  debrisQuarterBean: any = {};
  debrisHalfYearBean: any = {};
  debrisYearBean: any = {};

  escalatorHalfMonBean: any = {};
  escalatorHalfYearBean: any = {};
  escalatorQuarterBean: any = {};
  escalatorYearBean: any = {};

  hasGo = false;
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private storage: Storage,
    private httpService: HttpService,
    private navParams: NavParams,
    private commFunc: CommonFunc,
    private transfer: FileTransfer,
    private loading: LoadingController
  ) {
    /**
     * 先从缓存中取上次未提交的工作总结
     */
    this.storage.get(this.navParams.get("elevatorInfo").elevatorNum + "work-sum").then(data => {
      if (data == null) {
        return;
      }
      this.inspectionSummary = data.inspectionSummary;
      this.remarks = data.remarks;
    })
  }

  ionViewWillEnter() {
    this.storage.get("user").then(data => {
      this.user = data;
      this.param["userNameCn"] = this.user.userName;
      this.param["sendUserId"] = this.user.id;
    });
    this.elevatorInfo = this.navParams.get("elevatorInfo");
    if (this.elevatorInfo) {
      this.param["receivePartyId"] = this.elevatorInfo.usedPartyId;
      this.param["receiveUserId"] = this.elevatorInfo.safeUserId;
    }
    this.elevaType = this.navParams.get("ElevaType");

    //当前日期
    this.date = this.navParams.get("date");
    let date = moment(this.date).format("YYYYMMDD");

    //如果是巡检
    if (this.navParams.get("ElevaType") == "daily") {
      this.storage.get("dailyinspection").then(data => {
        this.param = data;
      });
    }
    //新增曳引电梯维保数据
    if (
      this.navParams.get("ElevaType") == "1" ||
      this.navParams.get("ElevaType") == "5"
    ) {
      this.storage.get(this.elevatorInfo.elevatorNum + "traction-half").then(data => {
        if (data.type >= 1) {
          this.param["tractionHalfMon"] = data;
          this.param["tractionHalfMon"].maintUserAddedList = this.navParams.get("maintUserAddedList");
          this.param["tractionHalfMon"].xcfhcs = this.navParams.get("xcfhcs");
          this.param["tractionHalfMon"].ldbh = this.navParams.get("ldbh");
          this.param["tractionHalfMon"].otherPhotos = this.navParams.get("otherPhotos");
        }
        if (data.type >= 2) {
          this.storage.get(this.elevatorInfo.elevatorNum + "traction-quarter").then(data => {
            this.param["tractionQuarter"] = data;
          });
        }
        if (data.type >= 3) {
          this.storage.get(this.elevatorInfo.elevatorNum + "traction-half-year").then(data => {
            this.param["tractionHalfYear"] = data;
          });
        }
        if (data.type >= 4) {
          this.storage.get(this.elevatorInfo.elevatorNum + "traction-year").then(data => {
            this.param["tractionYear"] = data;
          });
        }
        switch (data.type) {
          case "1":
            if (date >= this.elevatorInfo.nextHalfmonMaintDate) {
              this.param["tractionHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["tractionHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "2":
            if (date >= this.elevatorInfo.nextQuarterlyMainteDate) {
              this.param["tractionHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["tractionHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "3":
            if (date >= this.elevatorInfo.nextHalfyearMaintDate) {
              this.param["tractionHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["tractionHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "4":
            if (date >= this.elevatorInfo.nextYearMaintDate) {
              this.param["tractionHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["tractionHalfMon"].isInTimeMaint = "2";
            }
            break;
        }
      });
    }
    //获取液压电梯维保数据
    if (this.navParams.get("ElevaType") == "2") {
      this.storage.get(this.elevatorInfo.elevatorNum + "hydpress-half").then(data => {
        if (data.type >= 1) {
          this.param["hydpressHalfMon"] = data;
          this.param["hydpressHalfMon"].maintUserAddedList = this.navParams.get("maintUserAddedList");
          this.param["hydpressHalfMon"].xcfhcs = this.navParams.get("xcfhcs");
          this.param["hydpressHalfMon"].ldbh = this.navParams.get("ldbh");
          this.param["hydpressHalfMon"].otherPhotos = this.navParams.get("otherPhotos");
        }
        if (data.type >= 2) {
          this.storage.get(this.elevatorInfo.elevatorNum + "hydpress-quarter").then(data => {
            this.param["hydpressQuarter"] = data;
          });
        }
        if (data.type >= 3) {
          this.storage.get(this.elevatorInfo.elevatorNum + "hydpress-half-year").then(data => {
            this.param["hydpressHalfYear"] = data;
          });
        }
        if (data.type >= 4) {
          this.storage.get(this.elevatorInfo.elevatorNum + "hydpress-year").then(data => {
            this.param["hydpressYear"] = data;
          });
        }
        switch (data.type) {
          case "1":
            if (date >= this.elevatorInfo.nextHalfmonMaintDate) {
              this.param["hydpressHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["hydpressHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "2":
            if (date >= this.elevatorInfo.nextQuarterlyMainteDate) {
              this.param["hydpressHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["hydpressHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "3":
            if (date >= this.elevatorInfo.nextHalfyearMaintDate) {
              this.param["hydpressHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["hydpressHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "4":
            if (date >= this.elevatorInfo.nextYearMaintDate) {
              this.param["hydpressHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["hydpressHalfMon"].isInTimeMaint = "2";
            }
            break;
        }
      });
    }
    //新增杂物电梯维保数据
    if (this.navParams.get("ElevaType") == "3") {
      this.storage.get(this.elevatorInfo.elevatorNum + "debirs-half").then(data => {
        if (data.type >= 1) {
          this.param["debrisHalfMon"] = data;
          this.param["debrisHalfMon"].maintUserAddedList = this.navParams.get("maintUserAddedList");
          this.param["debrisHalfMon"].xcfhcs = this.navParams.get("xcfhcs");
          this.param["debrisHalfMon"].ldbh = this.navParams.get("ldbh");
          this.param["debrisHalfMon"].otherPhotos = this.navParams.get("otherPhotos");
        }
        if (data.type >= 2) {
          this.storage.get(this.elevatorInfo.elevatorNum + "debirs-quarter").then(data => {
            this.param["debrisQuarter"] = data;
          });
        }
        if (data.type >= 3) {
          this.storage.get(this.elevatorInfo.elevatorNum + "debirs-half-year").then(data => {
            this.param["debrisHalfYear"] = data;
          });
        }
        if (data.type >= 4) {
          this.storage.get(this.elevatorInfo.elevatorNum + "debirs-year").then(data => {
            this.param["debrisYear"] = data;
          });
        }
        switch (data.type) {
          case "1":
            if (date >= this.elevatorInfo.nextHalfmonMaintDate) {
              this.param["debrisHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["debrisHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "2":
            if (date >= this.elevatorInfo.nextQuarterlyMainteDate) {
              this.param["debrisHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["debrisHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "3":
            if (date >= this.elevatorInfo.nextHalfyearMaintDate) {
              this.param["debrisHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["debrisHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "4":
            if (date >= this.elevatorInfo.nextYearMaintDate) {
              this.param["debrisHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["debrisHalfMon"].isInTimeMaint = "2";
            }
            break;
        }
      });
    }

    //自动扶梯与自动人行道
    if (this.elevaType == "4" || this.elevaType == "6") {
      this.storage.get(this.elevatorInfo.elevatorNum + "escalator-half-mon").then(data => {
        if (data.type >= 1) {
          this.param["escalatorHalfMon"] = data;
          this.param["escalatorHalfMon"].maintUserAddedList = this.navParams.get("maintUserAddedList");
          this.param["escalatorHalfMon"].xcfhcs = this.navParams.get("xcfhcs");
          this.param["escalatorHalfMon"].ldbh = this.navParams.get("ldbh");
          this.param["escalatorHalfMon"].otherPhotos = this.navParams.get("otherPhotos");
        }
        if (data.type >= 2) {
          this.storage.get(this.elevatorInfo.elevatorNum + "escalator-quarter").then(data => {
            this.param["escalatorQuarter"] = data;
          });
        }
        if (data.type >= 3) {
          this.storage.get(this.elevatorInfo.elevatorNum + "escalator-half-year").then(data => {
            this.param["escalatorHalfYear"] = data;
          });
        }
        if (data.type >= 4) {
          this.storage.get(this.elevatorInfo.elevatorNum + "escalator-year").then(data => {
            this.param["escalatorYear"] = data;
          });
        }
        switch (data.type) {
          case "1":
            if (date >= this.elevatorInfo.nextHalfmonMaintDate) {
              this.param["escalatorHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["escalatorHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "2":
            if (date >= this.elevatorInfo.nextQuarterlyMainteDate) {
              this.param["escalatorHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["escalatorHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "3":
            if (date >= this.elevatorInfo.nextHalfyearMaintDate) {
              this.param["escalatorHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["escalatorHalfMon"].isInTimeMaint = "2";
            }
            break;
          case "4":
            if (date >= this.elevatorInfo.nextYearMaintDate) {
              this.param["escalatorHalfMon"].isInTimeMaint = "1";
            } else {
              this.param["escalatorHalfMon"].isInTimeMaint = "2";
            }
            break;
        }
      });
    }
  }

  //提交
  doConfirm() {
    if (!this.inspectionSummary) {
      const alert1 = this.alertCtrl.create({
        title: "警告",
        subTitle: "请输入总结后再提交",
        buttons: ["确定"]
      });
      alert1.present();
      return;
    }
    if (!this.commFunc.hasNetWork()) {
      const alert1 = this.alertCtrl.create({
        title: "警告",
        subTitle: "当前无网络，请确认连接后再提交",
        buttons: ["确定"]
      });
      alert1.present();
      return;
    }
    let load = this.loading.create({
      spinner: appConfig.spinner,
      duration: appConfig.timeout
    });

    load.present();
    //处理未上传的图片
    if (this.elevaType == "1" || this.elevaType == "5") {
      this.tractionHalfMonBean = this.param["tractionHalfMon"];
      this.tractionQuarterBean = this.param["tractionQuarter"];
      this.tractionHalfYearBean = this.param["tractionHalfYear"];
      this.tractionYearBean = this.param["tractionYear"];
      if (this.tractionHalfMonBean) {
        this.findToupLoad(this.tractionHalfMonBean);
      }
      if (this.tractionQuarterBean) {
        this.findToupLoad(this.tractionQuarterBean);
      }
      if (this.tractionHalfYearBean) {
        this.findToupLoad(this.tractionHalfYearBean);
      }
      if (this.tractionYearBean) {
        this.findToupLoad(this.tractionYearBean);
      }
    }
    if (this.elevaType == "2") {
      this.hydpressHalfMonBean = this.param["hydpressHalfMon"];
      this.hydpressQuarterBean = this.param["hydpressQuarter"];
      this.hydpressHalfYearBean = this.param["hydpressHalfYear"];
      this.hydpressYearBean = this.param["hydpressYear"];
      if (this.hydpressHalfMonBean) {
        this.findToupLoad(this.hydpressHalfMonBean);
      }
      if (this.hydpressQuarterBean) {
        this.findToupLoad(this.hydpressQuarterBean);
      }
      if (this.hydpressHalfYearBean) {
        this.findToupLoad(this.hydpressHalfYearBean);
      }
      if (this.hydpressYearBean) {
        this.findToupLoad(this.hydpressYearBean);
      }
    }
    if (this.elevaType == "3") {
      this.debrisHalfMonBean = this.param["debrisHalfMon"];
      this.debrisQuarterBean = this.param["debrisQuarter"];
      this.debrisHalfYearBean = this.param["debrisHalfYear"];
      this.debrisYearBean = this.param["debrisYear"];
      if (this.debrisHalfMonBean) {
        this.findToupLoad(this.debrisHalfMonBean);
      }
      if (this.debrisQuarterBean) {
        this.findToupLoad(this.debrisQuarterBean);
      }
      if (this.debrisHalfYearBean) {
        this.findToupLoad(this.debrisHalfYearBean);
      }
      if (this.debrisYearBean) {
        this.findToupLoad(this.debrisYearBean);
      }
    }
    if (this.elevaType == "4" || this.elevaType == "6") {
      this.escalatorHalfMonBean = this.param["escalatorHalfMon"];
      this.escalatorQuarterBean = this.param["escalatorQuarter"];
      this.escalatorHalfYearBean = this.param["escalatorHalfYear"];
      this.escalatorYearBean = this.param["escalatorYear"];
      if (this.escalatorHalfMonBean) {
        this.findToupLoad(this.escalatorHalfMonBean);
      }
      if (this.escalatorQuarterBean) {
        this.findToupLoad(this.escalatorQuarterBean);
      }
      if (this.escalatorHalfYearBean) {
        this.findToupLoad(this.escalatorHalfYearBean);
      }
      if (this.escalatorYearBean) {
        this.findToupLoad(this.escalatorYearBean);
      }
    }

    //开始正式往后走
    if (this.navParams.get("ElevaType") != "daily") {
      //维保
      let datas = new Date().getTime();
      this.maintenancenUsedTime =
        Math.floor((datas - this.date) / (60 * 1000)) + 1 + "";
      let maintenanceStartDate = moment(this.date).format("YYYYMMDDHHmmss");
      let maintenanceEndDate = moment(datas).format("YYYYMMDDHHmmss");
      //加一些数据到半月保里面
      if (this.elevaType == "1" || this.elevaType == "5") {
        this.tractionHalfMonBean.maintenanceStartDate = maintenanceStartDate;
        this.tractionHalfMonBean.maintenanceUserName = this.user.userName;
        this.tractionHalfMonBean.maintenanceUserId1 = this.user.id;
        this.tractionHalfMonBean.maintenancePartyId = this.user.partyId;
        this.tractionHalfMonBean.maintenanceEndDate = maintenanceEndDate;
        this.tractionHalfMonBean.maintenancenUsedTime = this.maintenancenUsedTime;
      }
      if (this.navParams.get("ElevaType") == "2") {
        this.hydpressHalfMonBean.maintenanceStartDate = maintenanceStartDate;
        this.hydpressHalfMonBean.maintenanceUserName = this.user.userName;
        this.hydpressHalfMonBean.maintenanceUserId1 = this.user.id;
        this.hydpressHalfMonBean.maintenancePartyId = this.user.partyId;
        this.hydpressHalfMonBean.maintenanceEndDate = maintenanceEndDate;
        this.hydpressHalfMonBean.maintenancenUsedTime = this.maintenancenUsedTime;
      }
      if (this.navParams.get("ElevaType") == "3") {
        this.debrisHalfMonBean.maintenanceStartDate = maintenanceStartDate;
        this.debrisHalfMonBean.maintenanceUserName = this.user.userName;
        this.debrisHalfMonBean.maintenanceUserId1 = this.user.id;
        this.debrisHalfMonBean.maintenancePartyId = this.user.partyId;
        this.debrisHalfMonBean.maintenanceEndDate = maintenanceEndDate;
        this.debrisHalfMonBean.maintenancenUsedTime = this.maintenancenUsedTime;
      }
      if (this.elevaType == "4" || this.elevaType == "6") {
        this.escalatorHalfMonBean.maintenanceStartDate = maintenanceStartDate;
        this.escalatorHalfMonBean.maintenanceEndDate = maintenanceEndDate;
        this.escalatorHalfMonBean.maintenanceUserName = this.user.userName;
        this.escalatorHalfMonBean.maintenanceUserId1 = this.user.id;
        this.escalatorHalfMonBean.maintenancePartyId = this.user.partyId;
        this.escalatorHalfMonBean.maintenancenUsedTime = this.maintenancenUsedTime;
      }
      //启动线程，看离线图片是否全部传完
      let _thiss = this;
      var timemer = setInterval(() => {
        if (_thiss.hasGo) {
          clearInterval(timemer);
          // return;
        }
        let checkRes: Boolean = false;
        if (_thiss.elevaType == "1" || _thiss.elevaType == "5") {
          checkRes =
            _thiss.check(_thiss.tractionHalfMonBean) &&
            _thiss.check(_thiss.tractionHalfYearBean) &&
            _thiss.check(_thiss.tractionQuarterBean) &&
            _thiss.check(_thiss.tractionYearBean);
        }
        if (_thiss.elevaType == "2") {
          checkRes =
            _thiss.check(_thiss.hydpressHalfMonBean) &&
            _thiss.check(_thiss.hydpressHalfYearBean) &&
            _thiss.check(_thiss.hydpressQuarterBean) &&
            _thiss.check(_thiss.hydpressYearBean);
        }
        if (_thiss.elevaType == "3") {
          checkRes =
            _thiss.check(_thiss.debrisHalfMonBean) &&
            _thiss.check(_thiss.debrisHalfYearBean) &&
            _thiss.check(_thiss.debrisQuarterBean) &&
            _thiss.check(_thiss.debrisYearBean);
        }
        if (_thiss.elevaType == "4" || _thiss.elevaType == "6") {
          checkRes =
            _thiss.check(_thiss.escalatorHalfMonBean) &&
            _thiss.check(_thiss.escalatorHalfYearBean) &&
            _thiss.check(_thiss.escalatorQuarterBean) &&
            _thiss.check(_thiss.escalatorYearBean);
        }
        if (checkRes) {
          const alert2 = _thiss.alertCtrl.create({
            title: "去签名",
            subTitle: "辛苦了，去签上您的大名",
            buttons: ["确定"]
          });
          alert2.present();

          //转换实体
          if (_thiss.elevaType == "1" || _thiss.elevaType == "5") {
            if (_thiss.tractionHalfMonBean) {
              /*现场防护措施*/
              if (_thiss.tractionHalfMonBean.xcfhcs && _thiss.tractionHalfMonBean.xcfhcs.length > 0) {
                _thiss.tractionHalfMonBean.xcfhcs = _thiss.tractionHalfMonBean.xcfhcs.join(',');
              } else {
                _thiss.tractionHalfMonBean.xcfhcs = '';
              }
              /*现场人员安全措施*/
              if (_thiss.tractionHalfMonBean.ldbh && _thiss.tractionHalfMonBean.ldbh.length > 0) {
                _thiss.tractionHalfMonBean.ldbh = _thiss.tractionHalfMonBean.ldbh.join(',');
              } else {
                _thiss.tractionHalfMonBean.ldbh = '';
              }
              /*其他照片集合*/
              if (_thiss.tractionHalfMonBean.otherPhotos && _thiss.tractionHalfMonBean.otherPhotos.length > 0) {
                  _thiss.tractionHalfMonBean.otherPhotos = _thiss.tractionHalfMonBean.otherPhotos.join(',');
              } else {
                  _thiss.tractionHalfMonBean.otherPhotos = '';
              }
              console.log(_thiss.tractionHalfMonBean.otherPhotos);
              if (_thiss.tractionHalfMonBean.maintUserAddedList && _thiss.tractionHalfMonBean.maintUserAddedList.length > 0) {
                _thiss.tractionHalfMonBean.maintUserAddedList.forEach(element => {
                    if (typeof (element.result.picUrl) == "undefined") {
                      element.result = JSON.parse(element.result);
                    }
                    if (typeof (element.result.picUrl) != "string" && element.result.picUrl.length > 0) {
                        element.result.picUrl = element.result.picUrl.join(',');
                    }
                  element.result = JSON.stringify(element.result);
                });
              }
              _thiss.param[
                "tractionHalfMon"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.tractionHalfMonBean
              );
            }
            if (_thiss.tractionQuarterBean) {

              _thiss.param[
                "tractionQuarter"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.tractionQuarterBean
              );
            }
            if (_thiss.tractionHalfYearBean) {
              _thiss.param[
                "tractionHalfYear"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.tractionHalfYearBean
              );
            }
            if (_thiss.tractionYearBean) {
              _thiss.param[
                "tractionYear"
              ] = _thiss.commFunc.convertBeanToString(_thiss.tractionYearBean);
            }

            _thiss.param["maintUserAddedList"] = _thiss.param["tractionHalfMon"].maintUserAddedList
          }

          if (_thiss.elevaType == "2") {
            if (_thiss.hydpressHalfMonBean) {
              if (_thiss.hydpressHalfMonBean.xcfhcs && _thiss.hydpressHalfMonBean.xcfhcs.length > 0) {
                _thiss.hydpressHalfMonBean.xcfhcs = _thiss.hydpressHalfMonBean.xcfhcs.join(',');
              } else {
                _thiss.hydpressHalfMonBean.xcfhcs = '';
              }
              if (_thiss.hydpressHalfMonBean.ldbh && _thiss.hydpressHalfMonBean.ldbh.length > 0) {
                _thiss.hydpressHalfMonBean.ldbh = _thiss.hydpressHalfMonBean.ldbh.join(',');
              } else {
                _thiss.hydpressHalfMonBean.ldbh = '';
              }

              /*其他照片集合*/
              if (_thiss.hydpressHalfMonBean.otherPhotos && _thiss.hydpressHalfMonBean.otherPhotos.length > 0) {
                  _thiss.hydpressHalfMonBean.otherPhotos = _thiss.hydpressHalfMonBean.otherPhotos.join(',');
              } else {
                  _thiss.hydpressHalfMonBean.otherPhotos = '';
              }

              if (_thiss.hydpressHalfMonBean.maintUserAddedList && _thiss.hydpressHalfMonBean.maintUserAddedList.length > 0) {
                _thiss.hydpressHalfMonBean.maintUserAddedList.forEach(element => {
                 // element.result.picUrl = element.result.picUrl.join(',');
                  //element.result = JSON.stringify(element.result);
                    if (typeof (element.result.picUrl) == "undefined") {
                        element.result = JSON.parse(element.result);
                    }
                    if (typeof (element.result.picUrl) != "string" && element.result.picUrl.length > 0) {
                        element.result.picUrl = element.result.picUrl.join(',');
                    }
                    element.result = JSON.stringify(element.result);
                });
              }
              _thiss.param[
                "hydpressHalfMon"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.hydpressHalfMonBean
              );
            }
            if (_thiss.hydpressQuarterBean) {
              _thiss.param[
                "hydpressQuarter"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.hydpressQuarterBean
              );
            }
            if (_thiss.hydpressHalfYearBean) {
              _thiss.param[
                "hydpressHalfYear"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.hydpressHalfYearBean
              );
            }
            if (_thiss.hydpressYearBean) {
              _thiss.param[
                "hydpressYear"
              ] = _thiss.commFunc.convertBeanToString(_thiss.hydpressYearBean);
            }
            _thiss.param["maintUserAddedList"] = _thiss.param["hydpressHalfMon"].maintUserAddedList
          }
          if (_thiss.elevaType == "3") {
            if (_thiss.debrisHalfMonBean) {
              if (_thiss.debrisHalfMonBean.xcfhcs && _thiss.debrisHalfMonBean.xcfhcs.length > 0) {
                _thiss.debrisHalfMonBean.xcfhcs = _thiss.debrisHalfMonBean.xcfhcs.join(',');
              } else {
                _thiss.debrisHalfMonBean.xcfhcs = '';
              }
              if (_thiss.debrisHalfMonBean.ldbh && _thiss.debrisHalfMonBean.ldbh.length > 0) {
                _thiss.debrisHalfMonBean.ldbh = _thiss.debrisHalfMonBean.ldbh.join(',');
              } else {
                _thiss.debrisHalfMonBean.ldbh = '';
              }

              /*其他照片集合*/
              if (_thiss.debrisHalfMonBean.otherPhotos && _thiss.debrisHalfMonBean.otherPhotos.length > 0) {
                  _thiss.debrisHalfMonBean.otherPhotos = _thiss.debrisHalfMonBean.otherPhotos.join(',');
              } else {
                  _thiss.debrisHalfMonBean.otherPhotos = '';
              }


              if (_thiss.debrisHalfMonBean.maintUserAddedList && _thiss.debrisHalfMonBean.maintUserAddedList.length > 0) {
                _thiss.debrisHalfMonBean.maintUserAddedList.forEach(element => {
                  //element.result.picUrl = element.result.picUrl.join(',');
                  //element.result = JSON.stringify(element.result);
                    if (typeof (element.result.picUrl) == "undefined") {
                        element.result = JSON.parse(element.result);
                    }
                    if (typeof (element.result.picUrl) != "string" && element.result.picUrl.length > 0) {
                        element.result.picUrl = element.result.picUrl.join(',');
                    }
                    element.result = JSON.stringify(element.result);
                });
              }
              _thiss.param[
                "debrisHalfMon"
              ] = _thiss.commFunc.convertBeanToString(_thiss.debrisHalfMonBean);
            }
            if (_thiss.debrisQuarterBean) {
              _thiss.param[
                "debrisQuarter"
              ] = _thiss.commFunc.convertBeanToString(_thiss.debrisQuarterBean);
            }
            if (_thiss.debrisHalfYearBean) {
              _thiss.param[
                "debrisHalfYear"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.debrisHalfYearBean
              );
            }
            if (_thiss.debrisYearBean) {
              _thiss.param["debrisYear"] = _thiss.commFunc.convertBeanToString(
                _thiss.debrisYearBean
              );
            }
            _thiss.param["maintUserAddedList"] = _thiss.param["debrisHalfMon"].maintUserAddedList
          }

          if (_thiss.elevaType == "4" || _thiss.elevaType == "6") {
            if (_thiss.escalatorHalfMonBean) {
              if (_thiss.escalatorHalfMonBean.xcfhcs && _thiss.escalatorHalfMonBean.xcfhcs.length > 0) {
                _thiss.escalatorHalfMonBean.xcfhcs = _thiss.escalatorHalfMonBean.xcfhcs.join(',');
              } else {
                _thiss.escalatorHalfMonBean.xcfhcs = '';
              }
              if (_thiss.escalatorHalfMonBean.ldbh && _thiss.escalatorHalfMonBean.ldbh.length > 0) {
                _thiss.escalatorHalfMonBean.ldbh = _thiss.escalatorHalfMonBean.ldbh.join(',');
              } else {
                _thiss.escalatorHalfMonBean.ldbh = '';
              }

              /*其他照片集合*/
              if (_thiss.escalatorHalfMonBean.otherPhotos && _thiss.escalatorHalfMonBean.otherPhotos.length > 0) {
                  _thiss.escalatorHalfMonBean.otherPhotos = _thiss.escalatorHalfMonBean.otherPhotos.join(',');
              } else {
                  _thiss.escalatorHalfMonBean.otherPhotos = '';
              }

              if (_thiss.escalatorHalfMonBean.maintUserAddedList && _thiss.escalatorHalfMonBean.maintUserAddedList.length > 0) {
                _thiss.escalatorHalfMonBean.maintUserAddedList.forEach(element => {
                  //element.result.picUrl = element.result.picUrl.join(',');
                  //element.result = JSON.stringify(element.result);
                    if (typeof (element.result.picUrl) == "undefined") {
                        element.result = JSON.parse(element.result);
                    }
                    if (typeof (element.result.picUrl) != "string" && element.result.picUrl.length > 0) {
                        element.result.picUrl = element.result.picUrl.join(',');
                    }
                    element.result = JSON.stringify(element.result);
                });
              }

              _thiss.param[
                "escalatorHalfMon"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.escalatorHalfMonBean
              );
            }
            if (_thiss.escalatorQuarterBean) {
              _thiss.param[
                "escalatorQuarter"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.escalatorQuarterBean
              );
            }
            if (_thiss.escalatorHalfYearBean) {
              _thiss.param[
                "escalatorHalfYear"
              ] = _thiss.commFunc.convertBeanToString(
                _thiss.escalatorHalfYearBean
              );
            }
            if (_thiss.escalatorYearBean) {
              _thiss.param[
                "escalatorYear"
              ] = _thiss.commFunc.convertBeanToString(_thiss.escalatorYearBean);
            }
            _thiss.param["maintUserAddedList"] = _thiss.param["escalatorHalfMon"].maintUserAddedList
          }

          _thiss.param["summary"] = _thiss.inspectionSummary;
          // _thiss.param["other"] = _thiss.other;
          _thiss.param["changeInfo"] = _thiss.remarks;
          _thiss.hasGo = true;
          load.dismiss();
          /**
           * 工作总结存放到缓存中，下次退出当前页面从缓存中取已填写内容
           * private inspectionSummary: string;
           private remarks: string;
           */
          this.storage.set(this.elevatorInfo.elevatorNum + "work-sum", {"inspectionSummary": this.inspectionSummary, "remarks": this.remarks});
          _thiss.navCtrl
            .push(SignPage, {
              elevatorNum: this.elevatorInfo.elevatorNum,
              ElevaType: _thiss.navParams.get("ElevaType"),
              params: _thiss.param
            })
            .then(() => {
              clearInterval(timemer);
            });
        }
      }, 1000);
    } else {
      //日常巡检
      const alert2 = this.alertCtrl.create({
        title: "去签名",
        subTitle: "辛苦了，去签上您的大名",
        buttons: ["确定"]
      });
      alert2.present();
      this.param["checkEndDt"] = moment().format("YYYYMMDDHHmmss");
      this.param["inspectionSummary"] = this.inspectionSummary;
      // this.param["other"] = this.other;
      this.param["remarks"] = this.remarks;
      load.dismiss();
      this.navCtrl.push(SignPage, {
        // elevatorNum: this.elevatorInfo.elevatorNum,
        ElevaType: this.navParams.get("ElevaType"),
        params: this.param
      });
    }
  }

  //检查是否有离线图片
  check(toBean) {
    let res = true;
    if (toBean) {
      try {
        for (const key in toBean) {
          if (toBean.hasOwnProperty(key)) {
            const element = toBean[key];
            if (element.picUrl && element.picUrl.length > 0) {
              let picArray = <Array<any>>element.picUrl;
              for (let i = 0; i < picArray.length; i++) {
                if ((picArray[i] + "").includes("file")) {
                  res = false;
                }
              }
            }
          }
        }
      } catch (error) {
        return res;
      }
    }
    return res;
  }

  //找到离线图片上传，并把结果回塞到对象里面
  findToupLoad(convertBean) {
    // this.tractionHalfMonBean = this.commFunc.convertBean(tractionHalfMon);
    for (const key in convertBean) {
      if (convertBean.hasOwnProperty(key)) {
        if (key == 'maintUserAddedList') {   // 是个 list
          const element2 = convertBean[key];
          if (element2) {
            element2.forEach(element => {
              if (element.result.picUrl && element.result.picUrl.length > 0) {
                let picArray = <Array<any>>element.result.picUrl;
                for (let i = 0; i < picArray.length; i++) {
                  let index = i;
                  if ((picArray[index] + "").includes("file")) {
                    const options: FileUploadOptions = {};
                    let serverUrl = encodeURI(
                      appConfig.IP_URL + appConfig.API.upload
                    );
                    options.fileKey = "file";
                    options.fileName = "test.jpg";
                    options.mimeType = "image/jpeg";
                    const fileTransfer: FileTransferObject = this.transfer.create();
                    fileTransfer
                      .upload(picArray[i], serverUrl, options)
                      .then(restul => {
                        let aa = JSON.parse(restul["response"]);
                        if (aa["status"] == 200) {
                          picArray[index] = aa["data"];
                        }
                      });
                  }
                }
              }
            });
          }

        } else if (key == 'xcfhcs') {
          const element1 = convertBean[key];
          if (element1 && element1.length > 0) {
            let picArray = <Array<any>>element1;
            for (let i = 0; i < picArray.length; i++) {
              let index = i;
              if ((picArray[index] + "").includes("file")) {
                const options: FileUploadOptions = {};
                let serverUrl = encodeURI(
                  appConfig.IP_URL + appConfig.API.upload
                );
                options.fileKey = "file";
                options.fileName = "test.jpg";
                options.mimeType = "image/jpeg";
                const fileTransfer: FileTransferObject = this.transfer.create();
                fileTransfer
                  .upload(picArray[i], serverUrl, options)
                  .then(restul => {
                    let aa = JSON.parse(restul["response"]);
                    if (aa["status"] == 200) {
                      picArray[index] = aa["data"];
                    }
                  });
              }
            }
          }
        } else if (key == 'ldbh') {
          const element1 = convertBean[key];
          if (element1 && element1.length > 0) {
            let picArray = <Array<any>>element1;
            for (let i = 0; i < picArray.length; i++) {
              let index = i;
              if ((picArray[index] + "").includes("file")) {
                const options: FileUploadOptions = {};
                let serverUrl = encodeURI(
                  appConfig.IP_URL + appConfig.API.upload
                );
                options.fileKey = "file";
                options.fileName = "test.jpg";
                options.mimeType = "image/jpeg";
                const fileTransfer: FileTransferObject = this.transfer.create();
                fileTransfer
                  .upload(picArray[i], serverUrl, options)
                  .then(restul => {
                    let aa = JSON.parse(restul["response"]);
                    if (aa["status"] == 200) {
                      picArray[index] = aa["data"];
                    }
                  });
              }
            }
          }
        } else {
          const element = convertBean[key];
          if (element.picUrl && element.picUrl.length > 0) {
            let picArray = <Array<any>>element.picUrl;
            for (let i = 0; i < picArray.length; i++) {
              let index = i;
              if ((picArray[index] + "").includes("file")) {
                const options: FileUploadOptions = {};
                let serverUrl = encodeURI(
                  appConfig.IP_URL + appConfig.API.upload
                );
                options.fileKey = "file";
                options.fileName = "test.jpg";
                options.mimeType = "image/jpeg";
                const fileTransfer: FileTransferObject = this.transfer.create();
                fileTransfer
                  .upload(picArray[i], serverUrl, options)
                  .then(restul => {
                    let aa = JSON.parse(restul["response"]);
                    if (aa["status"] == 200) {
                      picArray[index] = aa["data"];
                    }
                  });
              }
            }
          }
        }

      }
    }
  }
}
